/**
 * Copyright (C) 2006 OQube 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 * author: Arnaud Bailly - abailly AT oqube DOT com
 * created: 13 juin 2006
 */
package speculoos.beans;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;


/**
 * This class handles code generation for {@see speculoos.SyntheticMap} 
 * objects described in a JNDI configuration file.
 * Generated maps follow the following rules:
 * <ul>
 * <li>in the implementation, all attributes from all inherited maps (and class)
 * generate private field and get/set pairs, with the exception of 
 * the <strong>first</strong> Class instance which is used as 
 * the superclass of this implementation.</li>
 * </ul>
 * 
 * @author nono
 *
 */
public class MapsGenerator {

	/*
	 * where data is written to.
	 */
	private Writer outputInterface;

	/*
	 * output stream for implementation
	 */
	private Writer outputImpl;

	/*
	 * package name
	 */
	private String pack;
	
	/*
	 * an identifier associated with this generator.
	 */
	private String name;
	
	/**
	 * Generate interface and implementation for given maps.
	 * 
	 * @param map
	 * @throws IOException 
	 */
	public void generate(SyntheticMap map) throws IOException {
		StringTemplateGroup grp = new StringTemplateGroup("maps");
		StringTemplate iftpl = grp.getInstanceOf("templates/mapgenerator/interface");
		iftpl.setAttribute("map",map);
		setCommons(iftpl);
		outputInterface.write(iftpl.toString());
		iftpl = grp.getInstanceOf("templates/mapgenerator/implementation");
		iftpl.setAttribute("map",map);
		setCommons(iftpl);
		outputImpl.write(iftpl.toString());
	}


	private void setCommons(StringTemplate iftpl) {
		iftpl.setAttribute("date",new Date());
		iftpl.setAttribute("from",name);
		iftpl.setAttribute("package",pack);
	}


	/**
	 * @return Returns the outputImpl.
	 */
	public Writer getOutputImpl() {
		return outputImpl;
	}


	/**
	 * @param outputImpl The outputImpl to set.
	 */
	public void setOutputImpl(Writer outputImpl) {
		this.outputImpl = outputImpl;
	}


	/**
	 * @return Returns the outputInterface.
	 */
	public Writer getOutputInterface() {
		return outputInterface;
	}


	/**
	 * @param outputInterface The outputInterface to set.
	 */
	public void setOutputInterface(Writer outputInterface) {
		this.outputInterface = outputInterface;
	}


	/**
	 * @return Returns the pack.
	 */
	public String getPack() {
		return pack;
	}


	/**
	 * @param pack The pack to set.
	 */
	public void setPack(String pack) {
		this.pack = pack;
	}


	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
}
